#include <QtWidgets>
#include <QVariant>
#include <QColor>

// 自定义主窗口类，添加颜色属性
class AnimatedWindow : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(QColor color READ color WRITE setColor)
public:
    explicit AnimatedWindow(QWidget *parent = nullptr)
        : QWidget(parent), m_color(Qt::cyan)
    {
        // 初始化窗口属性
        setWindowTitle("窗口动画演示");
        setFixedSize(300, 200);
        setAutoFillBackground(true);
        updateColor();
        
        setupUI();
        setupAnimations();
    }

    QColor color() const { return m_color; }
    void setColor(const QColor &color)
    {
        m_color = color;
        updateColor(); // 更新背景颜色
    }

private slots:
    void toggleAnimation()
    {
        if(group.state() == QAbstractAnimation::Running) {
            group.pause();
            controlBtn->setText("继续");
        } else {
            group.start();
            controlBtn->setText("暂停");
        }
    }

private:
    void setupUI()
    {
        QVBoxLayout *layout = new QVBoxLayout(this);
        
        // 添加内容控件
        QLabel *label = new QLabel("窗口动画效果", this);
        label->setAlignment(Qt::AlignCenter);
        layout->addWidget(label);

        // 添加控制按钮
        controlBtn = new QPushButton("开始动画", this);
        connect(controlBtn, &QPushButton::clicked, this, &AnimatedWindow::toggleAnimation);
        layout->addWidget(controlBtn);
    }

    void setupAnimations()
    {
        // 窗口位置移动动画
        QPropertyAnimation *posAnim = new QPropertyAnimation(this, "geometry");
        posAnim->setDuration(1500);
        posAnim->setStartValue(QRect(-width(), 100, width(), height())); // 从屏幕左侧外进入
        posAnim->setEndValue(QRect(200, 100, width(), height()));
        posAnim->setEasingCurve(QEasingCurve::OutBounce);

        // 窗口颜色渐变动画
        QPropertyAnimation *colorAnim = new QPropertyAnimation(this, "color");
        colorAnim->setDuration(1500);
        colorAnim->setStartValue(QColor(Qt::cyan));
        colorAnim->setEndValue(QColor(255, 150, 50)); // 橙色

        // 窗口透明度动画
        QPropertyAnimation *opacityAnim = new QPropertyAnimation(this, "windowOpacity");
        opacityAnim->setDuration(1500);
        opacityAnim->setStartValue(0.3);
        opacityAnim->setEndValue(1.0);

        // 组合动画
        group.addAnimation(posAnim);
        group.addAnimation(colorAnim);
        group.addAnimation(opacityAnim);
    }

    void updateColor()
    {
        QPalette pal = palette();
        pal.setColor(QPalette::Window, m_color);
        setPalette(pal);
    }

    QColor m_color;
    QPushButton *controlBtn;
    QParallelAnimationGroup group;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    AnimatedWindow window;
    window.show();

    return app.exec();
}

#include "main.moc"